import PalaceUserModel from "../model/palaceUser";
import { getUserInfo } from "../thirdServices/palaceApi";
import log from "../utils/log";

/**5分钟执行一次更新登录状态 */

class PlaceUserStatusTask {
  async execute(page = 1) {
    const userListRes = await PalaceUserModel.getlist(1);
    for (let i = 0; i< userListRes.rows.length; i++) {
      const user = userListRes.rows[i];
      await this.checkUserStatus(user);
    }

    if (page * 20 < userListRes.count) {
      this.execute(page + 1);
    }
  }

  async checkUserStatus(user) {
    const userid = user.getDataValue('uuid');
    const token = user.getDataValue('accessToken');
    const params = {
      merchantId: 2655,
      merchantInfoId: 2655
    };
    try {
      await getUserInfo(token, params);
    } catch (error) {
      log.debug('【故宫】：用户' + userid +'登录失效');
      console.log(`${userid}:${JSON.stringify(error.response?.data)}`);
      try {
        PalaceUserModel.update({ status: 0 }, userid); 
      } catch (error) {
        console.log(error);
      }
    }
  }
}

export default PlaceUserStatusTask;